package com.carbeauty.entity;

import com.carbeauty.common.entity.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

/**
 * 字典实体类
 * 
 * 字典管理功能:
 * - 系统字典数据管理
 * - 分类管理
 * - 排序管理
 * - 层级关系管理
 * 
 * 支持的字典类型:
 * - 服务分类
 * - 材料分类
 * - 会员等级
 * - 客户来源
 * - 车辆品牌
 * - 车辆型号
 * - 车辆颜色
 * 
 * @author CarBeauty Team
 */
@Entity
@Table(name = "cb_dictionary")
public class Dictionary extends BaseEntity {
    
    /**
     * 字典类型
     */
    @NotBlank(message = "字典类型不能为空")
    @Size(max = 50, message = "字典类型长度不能超过50个字符")
    @Column(name = "dict_type", nullable = false, length = 50)
    private String dictType;
    
    /**
     * 字典编码
     */
    @NotBlank(message = "字典编码不能为空")
    @Size(max = 50, message = "字典编码长度不能超过50个字符")
    @Column(name = "dict_code", nullable = false, length = 50)
    private String dictCode;
    
    /**
     * 字典标签
     */
    @NotBlank(message = "字典标签不能为空")
    @Size(max = 100, message = "字典标签长度不能超过100个字符")
    @Column(name = "dict_label", nullable = false, length = 100)
    private String dictLabel;
    
    /**
     * 字典值
     */
    @Size(max = 100, message = "字典值长度不能超过100个字符")
    @Column(name = "dict_value", length = 100)
    private String dictValue;
    
    /**
     * 父级ID - 支持树形结构
     */
    @Column(name = "parent_id")
    private Long parentId = 0L;
    
    /**
     * 排序号
     */
    @Column(name = "sort_order")
    private Integer sortOrder = 0;
    
    /**
     * 是否默认选项
     */
    @Column(name = "is_default")
    private Boolean isDefault = false;
    
    /**
     * CSS样式类
     */
    @Size(max = 100, message = "CSS样式类长度不能超过100个字符")
    @Column(name = "css_class", length = 100)
    private String cssClass;
    
    /**
     * 列表样式类
     */
    @Size(max = 100, message = "列表样式类长度不能超过100个字符")
    @Column(name = "list_class", length = 100)
    private String listClass;
    
    /**
     * 字典描述
     */
    @Size(max = 200, message = "字典描述长度不能超过200个字符")
    @Column(name = "description", length = 200)
    private String description;
    
    // ==================== 构造方法 ====================
    
    public Dictionary() {
        super();
    }
    
    public Dictionary(String dictType, String dictCode, String dictLabel) {
        super();
        this.dictType = dictType;
        this.dictCode = dictCode;
        this.dictLabel = dictLabel;
        this.dictValue = dictCode;
    }
    
    public Dictionary(String dictType, String dictCode, String dictLabel, String dictValue) {
        super();
        this.dictType = dictType;
        this.dictCode = dictCode;
        this.dictLabel = dictLabel;
        this.dictValue = dictValue;
    }
    
    // ==================== Getter/Setter ====================
    
    public String getDictType() {
        return dictType;
    }
    
    public void setDictType(String dictType) {
        this.dictType = dictType;
    }
    
    public String getDictCode() {
        return dictCode;
    }
    
    public void setDictCode(String dictCode) {
        this.dictCode = dictCode;
    }
    
    public String getDictLabel() {
        return dictLabel;
    }
    
    public void setDictLabel(String dictLabel) {
        this.dictLabel = dictLabel;
    }
    
    public String getDictValue() {
        return dictValue;
    }
    
    public void setDictValue(String dictValue) {
        this.dictValue = dictValue;
    }
    
    public Long getParentId() {
        return parentId;
    }
    
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }
    
    public Integer getSortOrder() {
        return sortOrder;
    }
    
    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }
    
    public Boolean getIsDefault() {
        return isDefault;
    }
    
    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }
    
    public String getCssClass() {
        return cssClass;
    }
    
    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }
    
    public String getListClass() {
        return listClass;
    }
    
    public void setListClass(String listClass) {
        this.listClass = listClass;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    // ==================== 业务方法 ====================
    
    /**
     * 检查是否为根节点
     */
    public boolean isRoot() {
        return parentId == null || parentId == 0L;
    }
    
    /**
     * 检查是否为默认选项
     */
    public boolean isDefault() {
        return Boolean.TRUE.equals(isDefault);
    }
    
    /**
     * 设置为默认选项
     */
    public void setAsDefault() {
        this.isDefault = true;
    }
    
    /**
     * 取消默认选项
     */
    public void unsetDefault() {
        this.isDefault = false;
    }
    
    /**
     * 获取显示值 - 优先使用dictValue，如果为空则使用dictCode
     */
    public String getDisplayValue() {
        return dictValue != null && !dictValue.trim().isEmpty() ? dictValue : dictCode;
    }
    
    // ==================== 常用字典类型常量 ====================
    
    /**
     * 服务分类
     */
    public static final String TYPE_SERVICE_CATEGORY = "service_category";
    
    /**
     * 材料分类
     */
    public static final String TYPE_MATERIAL_CATEGORY = "material_category";
    
    /**
     * 会员等级
     */
    public static final String TYPE_MEMBER_LEVEL = "member_level";
    
    /**
     * 客户来源
     */
    public static final String TYPE_CUSTOMER_SOURCE = "customer_source";
    
    /**
     * 车辆品牌
     */
    public static final String TYPE_CAR_BRAND = "car_brand";
    
    /**
     * 车辆型号
     */
    public static final String TYPE_CAR_MODEL = "car_model";
    
    /**
     * 车辆颜色
     */
    public static final String TYPE_CAR_COLOR = "car_color";
    
    /**
     * 支付方式
     */
    public static final String TYPE_PAYMENT_METHOD = "payment_method";
    
    /**
     * 订单状态
     */
    public static final String TYPE_ORDER_STATUS = "order_status";
    
    /**
     * 用户角色
     */
    public static final String TYPE_USER_ROLE = "user_role";
}
